package com.czk.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/9/5 16:20
 */
public class HiveUtil {
    /**
     * 获取hive连接，->默认数据库
     *
     * @return
     */
    public static Connection getCon() {

        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:hive2://yth.bit-service.com:30100/default", "root", "bitservice");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取hive连接，指定数据库
     *
     * @param database
     * @return
     */
    public static Connection getCon(String database) {

        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:hive2://127.0.0.1:10000/" + database, "hive", "");
            System.out.println("连接hive");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param stmt
     * @param conn
     */
    public static void close(Statement stmt, Connection conn) {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @param conn
     */
    public static void close(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
